<?php
import('com.yamvc.model.Proxy');
import("com.yamvc.event.EventDispatcher");
import("com.yamvc.event.Event");
import("com.yamvc.event.EventListener");
class Model{
	/**
	 * Enter description here...
	 *
	 * @var Model
	 */
	private static $_instance;
	/**
	 * Enter description here...
	 *
	 * @return Model
	 */
	public static function instance(){
		if(self::$_instance==null){
			self::$_instance = new Model();
		}
		return self::$_instance;
	}
	
	private $_proxyes=array();
	/**
	 * Registers proxy with unique name
	 *
	 * @param string $proxyName
	 * @param Proxy $proxy
	 * @return Model
	 */
	public function registerProxy($proxyName,Proxy $proxy){
		if (key_exists($proxyName,$this->_proxyes)){
			throw new Exception("Proxy with this name already registered");
		}
		$this->_proxyes[$proxyName] = $proxy;
		return $this;
	}
	public function retreaveProxy($proxyName){
		if(!key_exists($proxyName,$this->_proxyes)){
			throw new Exception("Proxy with this name not registered");
		}
		return $this->_proxyes[$proxyName];
	}
	
	public function registerProxyes(DOMNodeList $proxyes){
	foreach ($proxyes as $proxyConf){
			Logger::getLogger("Application")->log("Registering proxy");
			$proxyClass=$proxyConf->firstChild->nodeValue;
			try{
				Logger::getLogger("Application")->log("Registering proxy ".$proxyClass);
				import($proxyClass);
				Logger::getLogger("Application")->log("Registering proxy ".$proxyClass);
				$proxyClass = explode(".",$proxyClass);
				$proxy = $proxyClass[sizeof($proxyClass)-1];
				$proxyInstance = new $proxy();
				
				$this->registerProxy($proxyConf->attributes->item(0)->value,$proxyInstance);
			}catch (ClassNotFoundException $e){
				throw $e;
			}
			//var_dump($mediatorClass);
			//var_dump($mediatorTemplateName);
		}
	}
	
	private function __construct(){
	}
	//// PROXY FUNCTIONS ////
	public function addEventListener($eventType,EventListener $eventListener){
		Controller::instance()->addEventListener($eventType,$eventListener);
	}
	public function dispatchEvent(Event $event){
		Controller::instance()->dispatchEvent($event);
	}
	
}

?>